//
//  TaskViewController.m
//  CarPark
//
//  Created by oceans on 2019/7/15.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "TaskViewController.h"
#import "TaskCollectionViewCell.h"
#import "ChipInstallListViewController.h"
#import "ReviewListViewController.h"
#import "CaptureListViewController.h"
#import "ChangeParkListViewController.h"
#import "XiaoXiViewController.h"

@interface TaskViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSDictionary *info;
@end

@implementation TaskViewController

- (instancetype)init {
    
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:klogin_notification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    [self loadData];
}

- (void)loadData {
    
    if ([UserModel shared].hasLogin == NO) {
        return;
    }
    defweakself
    [HttpRequest getURL:ktask_center paras:nil showProgress:NO successful:^(id  _Nullable result) {
        weakself.info = result[@"datas"];
        [weakself.collection reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"任务中心";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xiaoxi"] style:UIBarButtonItemStylePlain target:self action:@selector(messageButtonPressed)];
    self.navigationItem.rightBarButtonItem = right;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight-XTabBarHeight) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection registerNib:[UINib nibWithNibName:@"TaskCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"TaskCollectionViewCell"];
}

- (void)messageButtonPressed {
    
    XiaoXiViewController *con = [[XiaoXiViewController alloc] init];
    [Utility pushViewController:con];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(20, 15, 20, 15);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 4;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 35.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(ScreenWidth-30.0, 70.0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TaskCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TaskCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        cell.leftLabel.text = @"芯片安装任务";
        cell.rightLabel.text = [NSString stringWithFormat:@"%@", _info[@"task_chip_install_nums"]];
    }else if (indexPath.item == 1) {
        cell.leftLabel.text = @"更换芯片";
        cell.rightLabel.text = [NSString stringWithFormat:@"%@", _info[@"task_chip_change_nums"]];
    }else if (indexPath.item == 2) {
        cell.leftLabel.text = @"捕捉设备安装";
        cell.rightLabel.text = [NSString stringWithFormat:@"%@", _info[@"task_capture_install_nums"]];
    }else {
        cell.leftLabel.text = @"审核任务";
        cell.rightLabel.text = [NSString stringWithFormat:@"%@", _info[@"task_verify_nums"]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.item == 0) {
        
        ChipInstallListViewController *con = [[ChipInstallListViewController alloc] init];
        [Utility pushViewController:con];
    }else if (indexPath.item == 1) {
        
        ChangeParkListViewController *con = [[ChangeParkListViewController alloc] init];
        [Utility pushViewController:con];
    }else if (indexPath.item == 2) {
        
        CaptureListViewController *con = [[CaptureListViewController alloc] init];
        [Utility pushViewController:con];
    }else if (indexPath.item == 3) {
        
        ReviewListViewController *con = [[ReviewListViewController alloc] init];
        [Utility pushViewController:con];
    }
}

@end
